<template>
  <view :style="themeColor">
    <view class="page">
      <view class="flex benben-position-layout flex flex-wrap align-center signDonations_flex_0"
        :style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx', }">
        <view class='flex flex-wrap align-center justify-between flex-sub signDonations_fd0_0'>
          <view class='flex flex-wrap align-center signDonations_fd0_0_c0' @tap.stop="handleJumpDiy" data-type="back"
            data-url="1">
            <text class='fu-iconfont2  signDonations_fd0_0_c0_c0'>&#xE794;</text>
          </view>
          <view class='flex flex-wrap align-stretch justify-center'>
            <text class='signDonations_fd0_0_c1_c0'>{{$t('立即报名')}}</text>
          </view>
          <view class='flex flex-wrap align-center justify-end signDonations_fd0_0_c0'>
          </view>
        </view>

      </view>
      <view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
      <!---flex布局flex布局开始-->
      <view class="flex flex-direction flex-wrap align-stretch benben-flex-layout">
        <view class='flex flex-direction flex-wrap align-stretch signDonations_fd1_0'>
          <text class='signDonations_fd1_0_c0'>{{$t('功德主信息')}}</text>
          <view class='flex flex-wrap align-center justify-between signDonations_fd1_0_c1'>
            <text class='signDonations_fd1_0_c1_c0'>{{$t('姓名')}}</text>
            <input class='signDonations_fd1_0_c1_c1' type="text" :placeholder="$t('请输入')" confirm-type="done"
              :maxlength="-1" placeholder-style="color:rgba(153, 153, 153, 1);font-size:32rpx" v-model="name" />
          </view>
          <view class='flex flex-wrap align-center justify-between signDonations_fd1_0_c1'>
            <text class='signDonations_fd1_0_c1_c0'>{{$t('联系方式')}}</text>
            <input class='signDonations_fd1_0_c1_c1' type="text" :placeholder="$t('请输入')" confirm-type="done"
              :maxlength="20" placeholder-style="color:rgba(153, 153, 153, 1);font-size:32rpx" v-model="phone" />
          </view>
          <view class='flex flex-wrap align-center justify-between signDonations_fd1_0_c1'
            @tap.stop="pickerDiy1703598849413=true">
            <text class='signDonations_fd1_0_c1_c0'>{{$t('选择角色')}}</text>
            <view class='text-333 signDonations_fd1_0_c3_c1' :class="rolre ? '' : 'text-999'">{{rolre || $t('请选择')}}
            </view>
            <!-- <input class='signDonations_fd1_0_c3_c1' type="text" :placeholder="$t('请选择')" disabled confirm-type="done"
              :maxlength="-1" placeholder-style="color:rgba(153, 153, 153, 1);font-size:32rpx" v-model="rolre" /> -->
            <image class='signDonations_fd1_0_c3_c2' mode="aspectFit" :src='STATIC_URL+"41.png"'></image>
          </view>
        </view>
        <view class='flex flex-wrap align-center signDonations_fd1_1'>
        </view>
        <view class='flex flex-direction flex-wrap align-stretch signDonations_fd1_2'>
          <text class='signDonations_fd1_2_c0'>{{$t('物品信息')}}</text>
          <view class='flex flex-wrap align-center justify-between signDonations_fd1_2_c1'
            v-for="(item,index) in goods_list" :key="index">
            <view class='flex flex-wrap align-center justify-between signDonations_fd1_2_c1_c0'
              @click.stop="chooseGoods(item,index)">
              <input class='signDonations_fd1_2_c1_c0_c0' type="text" :placeholder="$t('请选择捐款物品')" confirm-type="done"
                v-model="item.name" :maxlength="-1" :disabled='true' placeholder-style="color:#999;font-size:28rpx" />
              <text class='fu-iconfont2  signDonations_fd1_2_c1_c0_c1'>&#xe78a;</text>
            </view>
            <image class='signDonations_fd1_2_c1_c1' @click.stop="clearNum(item,index)" mode="aspectFit"
              :src='STATIC_URL+"95.png"'></image>
            <view class='flex flex-wrap align-center signDonations_fd1_2_c1_c2'>
              <input class='signDonations_fd1_2_c1_c0_c0' type="number" :placeholder="$t('请输入捐款数量')" confirm-type="done"
                @input="chooseNum(item)" v-model="item.num" :maxlength="5"
                placeholder-style="color:#999;font-size:28rpx" />
            </view>
          </view>
        </view>
        <view class='flex flex-wrap align-center justify-center signDonations_fd1_0_c1' @click="addgoods">
          <button class='signDonations_fd1_3_c0'>{{$t('添加')}}</button>
        </view>
        <view class='flex flex-direction flex-wrap align-stretch signDonations_fd1_0'>
          <view class='flex flex-wrap align-center justify-between'>
            <text class='signDonations_fd1_0_c1_c0'>{{$t('捐款金额')}}</text>
            <!-- <input class='flex-sub signDonations_fd1_4_c0_c1' type="text" :placeholder="`0.00`" confirm-type="done"
              disabled :maxlength="-1" placeholder-style="color:rgba(153, 153, 153, 1);font-size:32rpx"
              v-model="custom" /> -->
            <view class="flex-sub signDonations_fd1_4_c0_c1">{{parseFloat(custom).toFixed(2)}}</view>
            <text class='signDonations_fd1_4_c0_c2'>$</text>
          </view>
        </view>
        <view class='flex flex-wrap align-center signDonations_fd1_1'>
        </view>
      </view>

      <!---flex布局flex布局结束-->
      <!---flex布局flex布局开始-->
      <view class="flex flex-direction flex-wrap align-stretch benben-flex-layout signDonations_flex_2">
        <view class='flex flex-wrap align-center'>
          <text class='signDonations_fd2_0_c0'>{{$t('请选择支付方式')}}</text>
        </view>
        <benben-select-diy ref="showSelectPopup1703169762016" class-name='flex flex-direction align-stretch flex'
          :items.sync="moneyType" v-model="money" default-type="aid" default-label="method_name" :allow-cancel='false'
          type="radio" :disabled='false'>
          <template v-for='(item,key0) in moneyType'>
            <view v-if="item.isSelected" class='flex justify-between flex signDonations_fd2_1_c0' :key="key0"
              @tap="$refs.showSelectPopup1703169762016.tapHandle(key0)">
              <view class='flex flex-wrap align-center'>
                <image class='signDonations_fd2_1_c0_c0_c0' mode="aspectFit" :src='item.thumb'></image>
                <text class='signDonations_fd2_1_c0_c0_c1'>{{item.method_name}}</text>
              </view>
              <image class='signDonations_fd2_1_c0_c1' mode="aspectFit" :src='STATIC_URL+"2.png"'></image>
            </view>
            <view v-else class='flex justify-between flex signDonations_fd2_1_c0' :key="key0"
              @tap="$refs.showSelectPopup1703169762016.tapHandle(key0)">
              <view class='flex flex-wrap align-center'>
                <image class='signDonations_fd2_1_c0_c0_c0' mode="aspectFit" :src='item.thumb'></image>
                <text class='signDonations_fd2_1_c0_c0_c1'>{{item.method_name}}</text>
              </view>
              <image class='signDonations_fd2_1_c0_c1' mode="aspectFit" :src='STATIC_URL+"3.png"'></image>
            </view>
          </template></benben-select-diy>
      </view>

      <!---flex布局flex布局结束-->
      <view
        class="flex flex-direction flex-wrap align-stretch justify-center benben-position-layout flex signDonations_flex_3">
        <view class='flex flex-wrap align-center justify-between flex-sub signDonations_fd3_0'>
          <view class='flex flex-wrap align-center'>
            <text class='signDonations_fd3_0_c0_c0'>{{$t('总计')}}：</text>
            <text class='signDonations_fd3_0_c0_c1'>￥</text>
            <text class=' signDonations_fd3_0_c0_c2'>
              <text class=' signDonations_price1_fd3_0_c0_c2'>{{ custom | frontPrice }}</text>
              <text class=' signDonations_price2_fd3_0_c0_c2'>{{ custom | laterPrice }}</text>
            </text>
          </view>
          <view class='flex flex-wrap align-center'>
            <button class='signDonations_fd3_0_c1_c0' @tap.stop="registerNowFunc()">{{$t('确认支付')}}</button>
          </view>
        </view>

      </view>
      <view :style="{height: '106rpx'}"></view>
      <!--选择角色开始 -->
      <benben-picker ref="benbenWritePickerCodepicker4" :visible.sync="pickerDiy1703598849413" :label.sync='rolre'
        :value.sync='role_id' :options='roleType' mode='selector' :mask-show='true' :timeout='true' :picker-height='88'
        default-type='aid' :default-props='{"label":"name","value":"aid"}'>
        <template #picker-header>
          <view class='flex flex-wrap align-center justify-between signDonations_picker4_0'>
            <text class='signDonations_picker4_0_c0' @tap="$refs.benbenWritePickerCodepicker4.cancel()">取消</text>
            <text></text>
            <text class='signDonations_picker4_0_c2' @tap="$refs.benbenWritePickerCodepicker4.pickerConfirm()">确认</text>
          </view>
        </template>
      </benben-picker>
      <!--选择角色结束 -->
      <!--选择性别开始 -->
      <benben-picker ref="benbenWritePickerCodepicker5" :visible.sync="pickerDiy1703520877653" :label.sync='typeName'
        :value.sync='typeId' :options='goodsList' mode='selector' :mask-show='true' :timeout='true' :picker-height='88'
        default-type='aid' :default-props='{"label":"name","value":"aid"}'>
        <template #picker-header>
          <view class='flex flex-wrap align-center justify-between signDonations_picker4_0'>
            <text class='signDonations_picker4_0_c0'
              @tap="$refs.benbenWritePickerCodepicker5.cancel()">{{$t('取消')}}</text>
            <text>{{$t('请选择捐款物品')}}</text>
            <text class='signDonations_picker4_0_c2' @tap="chooseIi">{{$t('确认')}}</text>
          </view>
        </template>
      </benben-picker>
      <!--选择性别开始 -->

    </view>
  </view>
</template>
<script>
  import {
    validate
  } from '@/common/utils/validate.js'
  export default {
    components: {},


    data() {
      return {
        "pickerDiy1703598849413": false,
        pickerDiy1703520877653: false,
        "name": "",
        "moneyType": [],
        "money": "",
        "custom": 0,
        "payType": [{
          "name": "微信支付",
          "value": "1",
          "image": require('@/static/images/71.png')
        }, {
          "name": "stripe",
          "value": "2",
          "image": require('@/static/images/72.png')
        }],
        "pay_id": "",
        "sign": [],
        "roleType": [],
        "role_id": "",
        "phone": "",
        "rolre": "",
        "order": "",
        "id": "",
        goodsList: [],
        tyoeId: "",
        typeName: '',
        goods_list: [{
          id: '',
          name: '',
          num: '',
          price: 0
        }],
        chooseIndex: '',
        typeId: ''
      };
    },
    computed: {
      themeColor() {
        return this.$store.getters.themeColor
      },

    },
    watch: {},
    onLoad(options) {
      let {
        order,
        id
      } = options
      if (order !== undefined) this.order = order
      if (id !== undefined) this.id = id
      this.getList()
      this.getRoleFunc()
      this.getPayFunc()
    },
    onUnload() {

    },
    onReady() {

    },
    onShow() {

    },
    onHide() {

    },
    onResize() {

    },
    onPullDownRefresh() {

    },
    onReachBottom(e) {

    },
    onPageScroll(e) {

    },
    methods: {
      addgoods() {
        this.goods_list.push({
          id: '',
          name: '',
          num: '',
          price: 0
        })
      },
      clearNum(item, index) {
        this.goods_list.splice(index)
        if (this.custom) {
          this.custom = parseFloat(this.custom) - parseFloat(item.num) * parseFloat(item.price)
        }
      },
      chooseNum(item) {
        this.custom = 0
        console.log(item)
        if (!item.id) {
          this.$message.info('请先选择捐款物品')
          item.num = 0
          return;
        }
        if (this.custom) {
          this.custom = parseFloat(this.custom) + parseFloat(item.num) * parseFloat(item.price)
        } else {
          this.custom = parseFloat(item.num) * parseFloat(item.price)
        }

      },
      chooseGoods(item, index) {
        this.chooseIndex = index
        this.pickerDiy1703520877653 = true
      },
      chooseIi() {
        this.$refs.benbenWritePickerCodepicker5.pickerConfirm()
        this.goods_list[this.chooseIndex].name = this.typeName
        this.goods_list[this.chooseIndex].id = this.typeId

        this.goods_list[this.chooseIndex].price = this.goodsList.filter(item => item.aid == this.typeId)[0].price
      },
      getList() {
        this.$api.post(global.apiUrls.post65bc8f9ad9b92, {
          aid: this.id,
          type: 2
        }).then(res => {
          if (res.data.code == 1) {
            this.goodsList = res.data.data
          }
        })
      },
      //获取角色
      async getRoleFunc() {
        //请求方法
        //数据验证

        let dataroleType = await this.$api.get(global.apiUrls.post6560315b21c3e, {

        });

        if (dataroleType.data.code != 1) {
          this.$message.info(dataroleType.data.msg);
          return
        }
        let inforoleType = dataroleType.data;
        this.roleType = inforoleType.data

      },
      //获取支付
      async getPayFunc() {
        //请求方法
        //数据验证

        let datamoneyType = await this.$api.get(global.apiUrls.post62fc4f70a9d44, {

        });

        if (datamoneyType.data.code != 1) {
          this.$message.info(datamoneyType.data.msg);
          return
        }
        let infomoneyType = datamoneyType.data;
        this.moneyType = infomoneyType.data

      },
      //立即报名
      async registerNowFunc() {
        if (!validate(this.name, 'require')) {
          this.$message.info(global.i18n.t('姓名不能为空'));
          return false;
        }
        if (!validate(this.phone, 'require')) {
          this.$message.info(global.i18n.t('手机号不能为空'));
          return false;
        }
        // if (!validate(this.phone, 'phone')) {
        //   this.$message.info(global.i18n.t('请输入正确的手机号'));
        //   return false;
        // }
        if (!validate(this.role_id, 'require')) {
          this.$message.info(global.i18n.t('请选择角色'));
          return false;
        }
        if (this.goods_list.length == 0) {
          this.$message.info(global.i18n.t('请选择物品信息'));
          return false;
        }
        // if (!validate(this.custom, 'require')) {
        //   this.$message.info(global.i18n.t('请输入捐款金额'));
        //   return false;
        // }
        if (!validate(this.money, 'require')) {
          this.$message.info(global.i18n.t('请选择支付的方式'));
          return false;
        }
        //请求方法
        //数据验证
        // 65bc8f9ad9b92  post6560363b08213
        let datasign = await this.$api.dbPost(global.apiUrls.post6560363b08213, {
          true_name: this.name,
          money: this.custom,
          mobile: this.phone,
          group_name: this.rolre,
          eventsinfo_id: this.id,
          goods_list: JSON.stringify(this.goods_list),
          pay_type: this.money
        });

        if (datasign.data.code != 1) {
          this.$message.info(datasign.data.msg);
          return
        }
        let infosign = datasign.data;
        this.sign = infosign.data
        this.getPay(this.sign.order_sn)

      },
      getPay(order_sn) {
        uni.showLoading({
          icon: 'loading',
          title: '数据正在提交中,请勿连续点击'
        })
        this.$api.post(global.apiUrls.post659b9ab8e7217, {
          order_sn: order_sn,
          pay_type: this.money
        }).then(res => {
          if (res.data.code == 1) {
            if (res.data.data.pay_type == '零元支付') {
              // 跳转支付成功
              this.$urouter.redirectTo(
                `/pages/shouye/registrationSuccessful/registrationSuccessful?oreder=${order_sn}`);
              uni.hideLoading()
              return;
            }
            if (res.data.data.pay_type == 'Stripe') {
              this.$urouter.navigateTo(`/pages/benben-built-in/web-view/web-view?webPath=${res.data.data.url}`);
              uni.hideLoading()
            } else {
              let datas = res.data.data
              uni.requestPayment({
                provider: 'wxpay',
                // #ifdef APP-PLUS
                orderInfo: datas,
                // #endif
                success: (e) => {
                  console.log('success', e)
                  if (e.errMsg == 'requestPayment:ok') {
                    this.$urouter.redirectTo(
                      `/pages/shouye/registrationSuccessful/registrationSuccessful?oreder=${order_sn}`);
                  }
                },
                fail: (e) => {
                  console.log('e.errMsg', e)
                  // #ifdef APP-PLUS
                  let failMsg = 'requestPayment:fail canceled';
                  // #endif
                  // #ifndef APP-PLUS
                  let failMsg = 'requestPayment:fail cancel';
                  // #endif
                  if (e.errMsg == failMsg) {
                    this.$message.info(this.$t('取消支付'));
                  } else {
                    this.$message.info(this.$t("支付失败,请稍后重试1"));
                  }
                  reject(e)
                },
                complete: () => {}
              });
              uni.hideLoading()
            }


          }
        })
      },
    }
  };
</script>
<style lang="scss" scoped>
  .page {
    width: 100vw;
    overflow-x: hidden;
    min-height: calc(100vh - var(--window-bottom));
    background: rgba(247, 243, 237, 1);
    background-size: 100% auto;
  }

  .signDonations_flex_0 {
    border-bottom: 1px solid #eee;
    background: rgba(247, 243, 237, 1);
    width: 750rpx;
    height: 88rpx;
    overflow: hidden;
    z-index: 10;
    top: 0rpx;
    background-size: 100% auto !important;
  }

  .signDonations_fd0_0_c1_c0 {
    font-size: 36rpx;
    font-weight: 700;
    color: #333333;
    line-height: 50rpx;
  }

  .signDonations_fd0_0_c0_c0 {
    font-size: 36rpx;
    color: #333;
  }

  .signDonations_fd0_0_c0 {
    width: 120rpx;
  }

  .signDonations_fd0_0 {
    padding: 0rpx 32rpx 0rpx 32rpx;
  }

  .signDonations_fd1_4_c0_c2 {
    color: #333333;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 32rpx;
    margin: 0rpx 0rpx 0rpx 21rpx;
  }

  .signDonations_fd1_4_c0_c1 {
    text-align: right;
    font-size: 32rpx;
    color: rgba(23, 26, 38, 1);
  }

  .signDonations_fd1_3_c0 {
    border: 1px solid rgba(152, 112, 64, 1);
    background: rgba(152, 112, 64, 0.1);
    border-radius: 40rpx;
    width: 686rpx;
    line-height: 80rpx;
    font-size: 32rpx;
    color: rgba(152, 112, 64, 1);
  }

  .signDonations_fd1_2_c1_c2 {
    background: rgba(255, 255, 255, 1);
    border-radius: 40rpx;
    background-size: 100% auto;
    padding: 20rpx 30rpx 20rpx 24rpx;
  }

  .signDonations_fd1_2_c1_c1 {
    width: 32rpx;
    height: 32rpx;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
    position: absolute;
    right: 0rpx;
    top: 0rpx;
  }

  .signDonations_fd1_2_c1_c0_c1 {
    font-size: 12rpx;
  }

  .signDonations_fd1_2_c1_c0_c0 {
    width: 196rpx;
  }

  .signDonations_fd1_2_c1_c0 {
    background: rgba(255, 255, 255, 1);
    border-radius: 40rpx;
    background-size: 100% auto;
    padding: 20rpx 30rpx 20rpx 24rpx;
    width: 420rpx;
  }

  .signDonations_fd1_2_c1 {
    margin: 16rpx 0rpx 0rpx 0rpx;
    position: relative;
  }

  .signDonations_fd1_2_c0 {
    color: #333333;
    font-size: 32rpx;
    font-weight: 500;
    line-height: 42rpx;
  }

  .signDonations_fd1_2 {
    margin: 32rpx 18rpx 0rpx 32rpx;
  }

  .signDonations_fd1_1 {
    background: rgba(235, 228, 217, 1);
    margin: 32rpx 32rpx 0rpx 32rpx;
    border-radius: 10rpx;
    height: 20rpx;
    background-size: 100% auto;
  }

  .signDonations_fd1_0_c3_c2 {
    width: 33rpx;
    height: 44rpx;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
  }

  .signDonations_fd1_0_c3_c1 {
    text-align: right;
    font-size: 32rpx;
    // color: #333;
    margin: 0rpx 0rpx 0rpx auto;
  }

  .signDonations_fd1_0_c1_c1 {
    text-align: right;
    font-size: 32rpx;
    color: #333;
  }

  .signDonations_fd1_0_c1_c0 {
    color: #333333;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 42rpx;
  }

  .signDonations_fd1_0_c1 {
    margin: 24rpx 0rpx 0rpx 0rpx;
  }

  .signDonations_fd1_0_c0 {
    color: #333333;
    font-size: 32rpx;
    font-weight: 500;
    line-height: 32rpx;
  }

  .signDonations_fd1_0 {
    margin: 32rpx 32rpx 0rpx 32rpx;
  }

  .signDonations_flex_2 {
    margin: 32rpx 0rpx 0rpx 0rpx;
    padding: 0rpx 32rpx 0rpx 32rpx;
  }

  .signDonations_fd2_1_c0_c1 {
    width: 36rpx;
    height: 36rpx;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
  }

  .signDonations_fd2_1_c0_c0_c1 {
    color: #333333;
    font-size: 28rpx;
    font-weight: 400;
    line-height: 53rpx;
  }

  .signDonations_fd2_1_c0_c0_c0 {
    width: 52rpx;
    height: 52rpx;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
    margin: 0rpx 16rpx 0rpx 0rpx;
  }

  .signDonations_fd2_1_c0 {
    border-bottom: 1px solid rgba(232, 229, 216, 1);
    padding: 32rpx 0rpx 32rpx 0rpx;
  }

  .signDonations_fd2_0_c0 {
    color: #333333;
    font-size: 32rpx;
    font-weight: 500;
    line-height: 56rpx;
  }

  .signDonations_flex_3 {
    background: rgba(255, 253, 250, 1);
    width: 750rpx;
    height: 106rpx;
    overflow: hidden;
    z-index: 10;
    bottom: calc(0rpx + var(--window-bottom));
    background-size: 100% auto;
  }

  .signDonations_fd3_0_c1_c0 {
    background: rgba(145, 47, 34, 1);
    border-radius: 40rpx;
    width: 234rpx;
    line-height: 80rpx;
    font-size: 32rpx;
    color: #fff;
  }

  .signDonations_price2_fd3_0_c0_c2 {
    font-size: 20rpx;
  }

  .signDonations_price1_fd3_0_c0_c2 {
    font-size: 32rpx;
  }

  .signDonations_fd3_0_c0_c2 {
    color: rgba(145, 47, 34, 1);
    font-weight: 600;
  }

  .signDonations_fd3_0_c0_c1 {
    color: #912F22;
    font-size: 20rpx;
    font-weight: 700;
    line-height: 36rpx;
  }

  .signDonations_fd3_0_c0_c0 {
    color: #1A1A1A;
    font-size: 28rpx;
    font-weight: 400;
    line-height: 32rpx;
  }

  .signDonations_fd3_0 {
    padding: 0rpx 32rpx 0rpx 32rpx;
  }

  .signDonations_picker4_0_c2 {
    color: rgba(145, 47, 34, 1);
  }

  .signDonations_picker4_0_c0 {
    color: rgba(153, 153, 153, 1);
  }

  .signDonations_picker4_0 {
    border-bottom: 1px solid #eee;
    background: #fff;
    padding: 0rpx 32rpx 0rpx 32rpx;
    line-height: 88rpx;
    border-radius: 25rpx 25rpx 0rpx 0rpx;
    background-size: 100% auto !important;
  }
</style>
